﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MPMusicVids.Data;
using System.Drawing.Drawing2D;

namespace MPMusicVids
{
    public partial class SongDetails : Form
    {
        string songID;
        DataManager dm = MusicVidsMain.dm; 

        public SongDetails(string songID)
        {
            InitializeComponent();
            this.songID = songID;
            loadDetails();
        }

        private void loadDetails()
        {
            try
            {
                Image aImg = Image.FromFile(dm.getThumb(songID));
                thumbPic.Image = aImg;
                SetImage(thumbPic);
            }
            catch { }
            textBox1.Text = dm.getSongName(songID);
        }

        //Generate new image dimensions
        public Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
        {
            double multiplier = 0;
            string layout;
            if (currH > currW) layout = "portrait";
            else layout = "landscape";

            switch (layout.ToLower())
            {
                case "portrait":
                    if (destH > destW)
                        multiplier = (double)destW / (double)currW;
                    else
                        multiplier = (double)destH / (double)currH;
                    break;
                case "landscape":
                    if (destH > destW)
                        multiplier = (double)destW / (double)currW;
                    else
                        multiplier = (double)destH / (double)currH;
                    break;
            }
            return new Size((int)(currW * multiplier), (int)(currH * multiplier));
        }

        private void SetImage(PictureBox pb)
        {
            try
            {
                Image img = pb.Image;
                Size imgSize = GenerateImageDimensions(img.Width, img.Height, pb.Width, pb.Height);
                Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);
                Graphics gfx = Graphics.FromImage(img);
                gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                pb.Image = null;
                pb.SizeMode = PictureBoxSizeMode.CenterImage;
                pb.Image = finalImg;
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dm.UpdateSongName(songID, textBox1.Text);
            this.Close();
        }
    }
}
